/**   
 * @Title: GestureLayout.java 
 * @Package android.framework.media 
 * @Description: TODO
 * @author jtf@163.com   
 * @date 2013-7-19 下午3:30:40 
 * @version V1.0   
 */
package android.framework.media;

import android.framework.R;

import android.content.Context;
import android.framework.util.L;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

/**
 * @ClassName: GestureLayout
 * @Description: TODO
 * @author jtf19@163.com
 * @date 2013-7-19 下午3:30:40
 * 
 */
public class GestureLayout extends RelativeLayout implements OnGestureListener {
	private final String  TAG = "GestureLayout";
	private int _currentPosition = -1;
	private float _offsetY = 0.0F;
	private final int _zoom = 1;
	private BasePlayActivity activity;
	private int curBrightness = 0;
	private int currentPosition = -1;
	private int directionalLock = 0;
	private int duration = -1;
	private boolean isLock = false;
	private float landscapeLimitSlope = 0.25F;
	private GestureDetector mGestureDetector = null;
	private int maxBrightness = 255;
	private int maxVolume;
	private float offsetY = 0.0F;
	private float portraitLimitSlope = 4.0F;
	private int progress_zoom = 5000;
	private final int zoom = 15;

	public GestureLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.activity = (BasePlayActivity) context;
		init();
	}

	public GestureLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		this.activity = (BasePlayActivity) context;
		init();
	}

	public GestureLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.activity = (BasePlayActivity) context;
		init();
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean isInPlaybackState() {
		if ((this.activity.getPlayController() != null)
				&& (!this.activity.getPlayController().isLoadingShown())
				&& (this.activity.getPlayController().getVideoView() != null)
				&& (this.activity.getPlayController().getVideoView()
						.isInPlaybackState()))
			return true;
		return false;
	}

	private boolean isPlayingAd() {
		if ((this.activity.getPlayController() != null)
				&& (this.activity.getPlayController().getVideoView() != null)
				&& (this.activity.getPlayController().isPlayingAd()))
			return true;
		return false;
	}

	private void toggleMediaControlsVisiblity() {
		if (this.activity.getPlayController().getMediaController() != null) {
			if (!this.activity.getPlayController().getMediaController()
					.isShowing())
				this.activity.getPlayController().getMediaController().hide();
		} else
			this.activity.getPlayController().getMediaController().show();
	}

	protected void init() {
		//inflate(this.activity, R.id.gesture_layout, this);
		this.mGestureDetector = new GestureDetector(this);
		this.mGestureDetector
				.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
					public boolean onDoubleTap(MotionEvent paramMotionEvent) {
						if (GestureLayout.this.isInPlaybackState())
							GestureLayout.this.activity.getPlayController()
									.getVideoView().toggleScreen();
						return true;
					}

					public boolean onDoubleTapEvent(MotionEvent paramMotionEvent) {
						return false;
					}

					public boolean onSingleTapConfirmed(
							MotionEvent paramMotionEvent) {
						if (GestureLayout.this.isPlayingAd())
							GestureLayout.this.activity.getPlayController()
									.handlerAdClick();
						else if (GestureLayout.this.isInPlaybackState()) {

							if (!GestureLayout.this.isLock)
								GestureLayout.this
										.toggleMediaControlsVisiblity();
							// UiUtils.notifyShortNormal(GestureLayout.this.activity,
							// 2131297100);
						}
						return isLock;
					}
				});
	}

	
	public void initializeData(){
		
	}
	
	public boolean isLock() {
		return this.isLock;
	}

	public boolean onTouchEvent(MotionEvent paramMotionEvent) {
		if (1 == paramMotionEvent.getAction()) {
			this.directionalLock = 0;
			if ((Math.abs(this.currentPosition - this._currentPosition) > 0)
					&& (isInPlaybackState()) && (!isPlayingAd())
					&& (!this.activity.getPlayController().isLive()))
				this.activity.getPlayController().getVideoView()
						.seekTo(this.currentPosition);
			this.currentPosition = -1;
			this.duration = -1;
			this._currentPosition = -1;
			this.activity.hideBrightnessLayout();
			this.activity.hideVolumeLayout();
			this.activity.hideprogressLayout();
		}
		return this.mGestureDetector.onTouchEvent(paramMotionEvent);
	}

}
